'use strict';

angular.module('Report')
    .controller('PeriodOutputController', ['$scope', '$http', '$q', '$stateParams', '$timeout', 'BaseModule', 'BaseEnum', 'BaseFunction','PeriodOutputService',
        function($scope, $http, $q, $stateParams, $timeout, BaseModule, BaseEnum, BaseFunction,PeriodOutputService) {
            $scope.dateStart = '';
            $scope.dateEnd = '';
            $scope.selectDateType = 0;//选中期间类型
            $scope.dateIsEnable = false;//选择日期是否可用
            $scope.dateType = {
                today: { Value: 0, Text: '今天' },
                yesterday: { Value: 1, Text: '昨天' },
                thisWeek: { Value: 2, Text: '本周' },
                thisMonth: { Value: 3, Text: '本月' },
                diyDate: { Value: 4, Text: '自定义' }
            };
            $scope.sumTransInQty = 0;
            $scope.sumCompleteQty = 0;
            $scope.sumEWIP = 0;
            $scope.sumEOH = 0;
            var year,month,day;//现在年月日
            var now = new Date();                    //当前日期     
            var nowDayOfWeek = now.getDay();         //今天本周的第几天
            if(nowDayOfWeek===0){
                nowDayOfWeek=7;
            }     
            var nowDay = now.getDate();              //当前日     
            var nowMonth = now.getMonth();           //当前月     
            var nowYear = now.getFullYear();             //当前年 

            BaseModule.initModule($scope, '_PeriodOutput');
            $scope.Module._PeriodOutput.LoadMode = BaseEnum.LoadMode.CacheAndPaging;
            $scope.Module._PeriodOutput.PageSize = 15;


            $scope.selectType = function() {
                if ($scope.selectDateType === 4) {
                    $scope.dateIsEnable = 'true';
                }else{
                    $scope.dateIsEnable = 'false';
                }
            };

            //格式化日期：yyyy-MM-dd     
            function formatDate(date) {      
                var myyear = date.getFullYear();     
                var mymonth = date.getMonth()+1;     
                var myweekday = date.getDate();      
                     
                if(mymonth < 10){     
                    mymonth = "0" + mymonth;     
                }      
                if(myweekday < 10){     
                    myweekday = "0" + myweekday;     
                }     
                return (myyear+"-"+mymonth + "-" + myweekday);      
            }
            //获得某月的天数     
            function getMonthDays(myMonth){     
                var monthStartDate = new Date(nowYear, myMonth, 1);      
                var monthEndDate = new Date(nowYear, myMonth + 1, 1);      
                var   days   =   (monthEndDate   -   monthStartDate)/(1000   *   60   *   60   *   24);      
                return   days;      
            }  
            //获取当天日期
            function getToday(){
                return formatDate(now);
            }
            //获取昨天日期
            function getYesterday(){
                var yesterdayDate = new Date(nowYear, nowMonth, nowDay - 1);      
                return formatDate(yesterdayDate); 
            }
            //获得本周的开始日期     
            function getWeekStartDate() {      
                var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + 1);      
                return formatDate(weekStartDate);     
            }      
                
            //获得本周的结束日期     
            function getWeekEndDate() {      
                var weekEndDate = new Date(nowYear, nowMonth, nowDay + (7 - nowDayOfWeek));      
                return formatDate(weekEndDate);     
            }      
                
            //获得本月的开始日期     
            function getMonthStartDate(){     
                var monthStartDate = new Date(nowYear, nowMonth, 1);      
                return formatDate(monthStartDate);     
            }     
                
            //获得本月的结束日期     
            function getMonthEndDate(){     
                var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));      
                return formatDate(monthEndDate);     
            }

            console.log("今天"+getToday()+'\n'+
                        "昨天"+getYesterday()+'\n'+
                        "本周开始"+getWeekStartDate()+'\n'+
                        "本周结束"+getWeekEndDate()+'\n'+
                        "本月开始"+getMonthStartDate()+'\n'+
                        "本月结束"+getMonthEndDate());       


            $scope.search = function() {
                var startDay,endDay;
                if($scope.selectDateType===0){//今天
                    startDay = getToday();
                    endDay = getToday();
                }
                if ($scope.selectDateType===1) {//昨天
                    startDay = getYesterday();
                    endDay = getYesterday();
                }
                if ($scope.selectDateType===2) {//本周
                    startDay = getWeekStartDate();
                    endDay = getWeekEndDate();
                }
                if ($scope.selectDateType===3) {//本月
                    startDay = getMonthStartDate();
                    endDay = getMonthEndDate();
                }
                if ($scope.selectDateType===4){//自定义
                    startDay = formatDate(new Date($scope.dateStart));
                    endDay = formatDate(new Date($scope.dateEnd));
                }
                BaseFunction.loading(true); 
                
                PeriodOutputService.getPeriodOutputByDate(startDay,endDay,$scope.Module.Department.FocusedRecord._id).then(function (result) {
                    BaseFunction.loading(false); 
                    $scope.sumTransInQty = result.sumTransInQty;
                    $scope.sumCompleteQty = result.sumCompleteQty;
                    $scope.sumEWIP = result.sumEWIP;
                    $scope.sumEOH = result.sumEOH;
                    $scope.Module._PeriodOutput.Records = result.listData; 
                    $scope.Module._PeriodOutput.emitOnLoadEvent();
                    
                },function(error){
                    BaseFunction.loading(false); 
                    if(error){
                       BaseFunction.alert(error.message, error.title); 
                    }
                    
                });
            }

        }
])